package com.smart.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import com.smart.entity.SysUser;
import com.smart.entity.WorkOrder;

import cn.hutool.core.util.StrUtil;
import com.smart.req.InspectionParam;
import org.springframework.beans.BeanUtils;

import com.smart.common.util.SysUserUtils;
import com.smart.entity.SysInspectionRecord;
import org.springframework.stereotype.Service;
import com.smart.mapper.InspectionRecordMapper;
import com.smart.service.InspectionRecordService;
import com.smart.service.WorkOrderService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * @author lgb
 * @date 2020年6月9日 下午5:03:37
 * Copyright (C) 深圳工勘科技
 * @说明: 巡检记录
 **/
@Service
public class InspectionRecordServiceImpl extends ServiceImpl<InspectionRecordMapper,
		SysInspectionRecord> implements InspectionRecordService{

	@Resource
	private WorkOrderService workOrderService;
	/**
	 * (non-Javadoc)
	 * @see com.smart.service.InspectionRecordService#save(com.smart.req.InspectionParam)
	 * 保存巡检记录
	 **/
	@Override
	@Transactional
	public int save(InspectionParam param) {
		SysInspectionRecord record = new SysInspectionRecord();
		SysUser user = SysUserUtils.getSysUser();
		BeanUtils.copyProperties(param,record);
		if(!CollectionUtils.isEmpty(param.getImgs())){
			record.setImgs(String.join(",",param.getImgs()));
		}
		int res = 0;
		if(param.getWorkId() == null){
			record.setUserId(user.getId());
			record.setCreateTime(new Date());
			record.setModifyTime(new Date());
			record.setStatus(0);
			res = baseMapper.insert(record);
			WorkOrder workOrder = new WorkOrder();
            workOrder.setCreateTime(System.currentTimeMillis());
            workOrder.setReason("新建工单!");
            workOrder.setStatus(0);
            workOrder.setType(2);//1保养管理
            workOrder.setWorkId(record.getId().intValue());
            workOrder.setCarNo(record.getCarNo());
            workOrder.setCarType(record.getCarType());
            workOrder.setCreateId(user.getId().intValue());
            workOrder.setCreateName(user.getNickName());
            workOrder.setImgs(record.getImgs());
//            workOrder.setSiteId(record.getSiteId().intValue());
//            workOrder.setSiteName(record.getSiteName());
            workOrderService.saveEntity(workOrder);
		}else{
			record.setId(param.getWorkId().longValue());
			record.setStatus(0);
			baseMapper.updateById(record);
			LambdaQueryWrapper<WorkOrder> wrapper = new LambdaQueryWrapper<>();
        	wrapper.eq(WorkOrder::getWorkId,param.getWorkId()).eq(WorkOrder::getType,2);//车辆管理
        	WorkOrder w = workOrderService.getBaseMapper().selectOne(wrapper);
        	w.setStatus(0);
        	res=workOrderService.update(w);
		}
		return res;
	}

	/**
	 * (non-Javadoc)
	 * @see com.smart.service.InspectionRecordService#findAll()
	 * 保存巡检分页
	 **/
	@Override
	@Transactional(readOnly=true)
	@SuppressWarnings("unchecked")
	public IPage<SysInspectionRecord> findPage(int pageNum,int pageSize,String createTime) {
		LambdaQueryWrapper<SysInspectionRecord> wrapper=new LambdaQueryWrapper<>();
		SysUser details = SysUserUtils.getSysUser();
		wrapper.eq(SysInspectionRecord::getUserId,details.getId());
		if (StrUtil.isNotEmpty(createTime)) {
            wrapper.gt(SysInspectionRecord::getCreateTime,createTime+" 00:00:00");
            wrapper.le(SysInspectionRecord::getCreateTime,createTime+" 23:59:59");
        }
		wrapper.orderByDesc(SysInspectionRecord::getCreateTime);
		return page(new Page<SysInspectionRecord>(pageNum,pageSize),wrapper);
	}

	
	
}